package com.vtquang.mymaps.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.vtquang.mymaps.R;
import com.vtquang.mymaps.StepDirection;

public class DirectionAdapter extends BaseAdapter {
	private static ArrayList<StepDirection> itemDetailsrrayList;
	private LayoutInflater l_Inflater;

	public DirectionAdapter(Context context, ArrayList<StepDirection> results) {
		itemDetailsrrayList = results;
		l_Inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return itemDetailsrrayList.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return itemDetailsrrayList.get(arg0);
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolde holder;
		if (convertView == null) {
			convertView = l_Inflater
					.inflate(R.layout.directiondialog_row, null);
			holder = new ViewHolde();
			holder._tvStt = (TextView)convertView.findViewById(R.id.tvStt);
			holder._tvInstructions = (TextView) convertView
					.findViewById(R.id.tvInstructions);
			holder._tvDuration = (TextView) convertView
					.findViewById(R.id.tvDuration);
			holder._tvDistance = (TextView) convertView
					.findViewById(R.id.tvDistance);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolde) convertView.getTag();
		}

		StepDirection step = itemDetailsrrayList.get(position);
		if (step != null) {
			holder._tvStt.setText(formatStt(position + 1));
			holder._tvInstructions.setText(
					Html.fromHtml(step.get_instructions()),
					TextView.BufferType.SPANNABLE);
			holder._tvDuration.setText("Thời gian : " + step.get_duration());
			holder._tvDistance.setText("Khoảng : " + step.get_distance());
		}

		return convertView;
	}

	private class ViewHolde {
		TextView _tvStt;
		TextView _tvInstructions;
		TextView _tvDuration;
		TextView _tvDistance;
	}
	
	private String formatStt(int stt){
		String result;
		
		result = String.format("%-4d", stt); 
		result = result.replaceFirst(" ", ".");
		
		return result;
	}
}
